package com.ydgk.controller;

import com.ydgk.bean.Detail;
import com.ydgk.service.DetailService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/detail")
@Controller
public class DetailController {
    @Autowired
    private DetailService detailService;

    private static final String UPLOAD_DIRECTORY = "upload/detail";
    public static Map<String,MultipartFile> fileForDetail = new HashMap<String, MultipartFile>();

    @ModelAttribute
    public void getDetail(@RequestParam(value = "did" , required = false , defaultValue = "0") Integer did, Map map){
        if(did != 0){
            Detail detail = detailService.findById(did);
            map.put("detail",detail);
        }
    }

    @ResponseBody
    @RequestMapping("/detailUpload")
    public AjaxResult detailUpload(@RequestParam("file") MultipartFile file) throws IOException {
        this.fileForDetail.put(file.getOriginalFilename(),file);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/fileDelete")
    public AjaxResult fileDelete(String name) throws IOException {
        this.fileForDetail.remove(name);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteDetail/{did}", method = RequestMethod.DELETE)
    public AjaxResult deleteDetail(@PathVariable("did")Integer did, Integer fid, Detail detail, HttpServletRequest request){
        ServletContext application = request.getServletContext();
        AjaxResult ajaxResult = new AjaxResult();
        int i = detailService.deleteByIdAndFid(did,fid);
        if(i > 0){
            String dphoto = detail.getDphoto();
            new File(application.getRealPath("/"+dphoto)).delete();
            ajaxResult.setSuccess(true);
        }else{
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/insertDetail")
    public AjaxResult insertDetail(HttpServletRequest request, Integer fid) throws IOException {
        Collection<MultipartFile> details = fileForDetail.values();
        Detail detail = new Detail();
        String detailname = "";
        for (MultipartFile file : details) {
            detailname = Tool.upload(request,file,UPLOAD_DIRECTORY);
            detail.setDphoto(UPLOAD_DIRECTORY + "/" + detailname);
            detail.setF_fid(fid);
            detailService.insert(detail);
        }
        clear();
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    public static void clear(){
        fileForDetail.clear();
    }
}
